using System;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Collections.Generic;
using System.Reflection;

namespace DocumentManagement
{
  /// <summary>
  /// Tletters Data Object Class
  /// </summary>
  public partial class DOTletters
  {
    #region Class Constant declarations
    		/// <summary>
		/// The Table Column for the Tletters letterid
		/// </summary>
		public const string Rowletterid = "letterid";

		/// <summary>
		/// The Table Column for the Tletters ilpnumber
		/// </summary>
		public const string Rowilpnumber = "ilpnumber";

		/// <summary>
		/// The Table Column for the Tletters letterreference
		/// </summary>
		public const string Rowletterreference = "letterreference";

		/// <summary>
		/// The Table Column for the Tletters letternotes
		/// </summary>
		public const string Rowletternotes = "letternotes";

		/// <summary>
		/// The Table Column for the Tletters letteruser
		/// </summary>
		public const string Rowletteruser = "letteruser";

		/// <summary>
		/// The Table Column for the Tletters senttoprimaryemail
		/// </summary>
		public const string Rowsenttoprimaryemail = "senttoprimaryemail";

		/// <summary>
		/// The Table Column for the Tletters senttosecondaryemail
		/// </summary>
		public const string Rowsenttosecondaryemail = "senttosecondaryemail";


#region Stored Procedures
private const string m_StoredProcedureConstCreate = "spTletters_Create";
private const string m_StoredProcedureConstDelete = "spTletters_Delete";
private const string m_StoredProcedureConstRetrieve = "spTletters_Retrieve";
private const string m_StoredProcedureConstRetrieveall = "spTletters_Retrieveall";
private const string m_StoredProcedureConstUpdate = "spTletters_Update";
#endregion
    
    
    #endregion

    #region Class Variable declarations
    	#region DO Variables
		private int m_letterid;
		private string m_ilpnumber;
		private string m_letterreference;
		private string m_letternotes;
		private string m_letteruser;
		private string m_senttoprimaryemail;
		private string m_senttosecondaryemail;
	#endregion
    
	#region Default Variables
	private SQLDataObject sqlData;
	private List<DOTletters> m_Tletterss = new List<DOTletters>();
	#endregion
	
    #endregion

    #region Class Constructors & Destructors
    /// <summary>
    /// Class Constructor
    /// </summary>
    public DOTletters() { }
    #endregion

    #region Class Properties
    #region DO Properties
		/// <summary>
		/// letterid
		/// </summary>
		/// <remarks>
		/// This property contains the Tletters letterid
		/// </remarks>
		public int letterid
		{
			get { return (this.m_letterid); }
			set { this.m_letterid = value; }
		}

		/// <summary>
		/// ilpnumber
		/// </summary>
		/// <remarks>
		/// This property contains the Tletters ilpnumber
		/// </remarks>
		public string ilpnumber
		{
			get { return (this.m_ilpnumber); }
			set { this.m_ilpnumber = value; }
		}

		/// <summary>
		/// letterreference
		/// </summary>
		/// <remarks>
		/// This property contains the Tletters letterreference
		/// </remarks>
		public string letterreference
		{
			get { return (this.m_letterreference); }
			set { this.m_letterreference = value; }
		}

		/// <summary>
		/// letternotes
		/// </summary>
		/// <remarks>
		/// This property contains the Tletters letternotes
		/// </remarks>
		public string letternotes
		{
			get { return (this.m_letternotes); }
			set { this.m_letternotes = value; }
		}

		/// <summary>
		/// letteruser
		/// </summary>
		/// <remarks>
		/// This property contains the Tletters letteruser
		/// </remarks>
		public string letteruser
		{
			get { return (this.m_letteruser); }
			set { this.m_letteruser = value; }
		}

		/// <summary>
		/// senttoprimaryemail
		/// </summary>
		/// <remarks>
		/// This property contains the Tletters senttoprimaryemail
		/// </remarks>
		public string senttoprimaryemail
		{
			get { return (this.m_senttoprimaryemail); }
			set { this.m_senttoprimaryemail = value; }
		}

		/// <summary>
		/// senttosecondaryemail
		/// </summary>
		/// <remarks>
		/// This property contains the Tletters senttosecondaryemail
		/// </remarks>
		public string senttosecondaryemail
		{
			get { return (this.m_senttosecondaryemail); }
			set { this.m_senttosecondaryemail = value; }
		}
		#endregion
    
	#region Default Properties
	public List<DOTletters> Tletterss
	{
	  get { return m_Tletterss; }
	  set { m_Tletterss = value; }
	}    
	#endregion
	
    #endregion

    #region Class Methods
    public DataSet Retrieve()
    {
      DataSet returnDS = new DataSet();
      sqlData = new SQLDataObject();
      sqlData.ExecuteDataSetSQL(m_StoredProcedureConstRetrieve + " @" + Rowletterid + "=" + this.letterid);
      return returnDS;
    }

    public bool RetrieveAll()
    {
      sqlData = new SQLDataObject();
      DataSet resultDS = sqlData.ExecuteDataSetSQL(m_StoredProcedureConstRetrieveall);
      return SetClassPropertiesListFromDataSet(resultDS);
    }

    public bool Create()
    {
      bool success = false;
      string sVariables = "";
      sqlData = new SQLDataObject();

      Type type = this.GetType();
      PropertyInfo[] propInfo = type.GetProperties();

      for (int i = 0; i < propInfo.Length; i++)
      {
        if (propInfo[i].PropertyType.Name.IndexOf("List") == -1)
        {
          sVariables += "@" + propInfo[i].Name + " = ";
          if (propInfo[i].PropertyType.Name == "String")
            sVariables += "'" + propInfo[i].GetValue(this, null) + "', ";
          else
            sVariables += propInfo[i].GetValue(this, null) + ", ";
        }
      }

      success = sqlData.ExecuteNonQuerySQL(m_StoredProcedureConstCreate + " " + sVariables.Substring(0, sVariables.Length - 2));

      return success;
    }

    public bool Update()
    {
      bool success = false;
      string sVariables = "";
      sqlData = new SQLDataObject();

      Type type = this.GetType();
      PropertyInfo[] propInfo = type.GetProperties();

      for (int i = 0; i < propInfo.Length; i++)
      {
        if (propInfo[i].PropertyType.Name.IndexOf("List") == -1)
        {
          sVariables += "@" + propInfo[i].Name + " = ";
          if (propInfo[i].PropertyType.Name == "String")
            sVariables += "'" + propInfo[i].GetValue(this, null) + "', ";
          else
            sVariables += propInfo[i].GetValue(this, null) + ", ";
        }
      }

      success = sqlData.ExecuteNonQuerySQL(m_StoredProcedureConstUpdate + " " + sVariables.Substring(0, sVariables.Length - 2));

      return success;
    }

    private bool SetClassPropertiesFromDataSet(DataSet resultDS)
    {
      bool success = false;
      if (resultDS.Tables[0] != null)
      {
        if (resultDS.Tables[0].Rows.Count > 0)
        {
          foreach (DataRow dr in resultDS.Tables[0].Rows)
          {
					if (dr[Rowletterid].ToString() != "")
			this.letterid = int.Parse(dr[Rowletterid].ToString());

		if (dr[Rowilpnumber].ToString() != "")
			this.ilpnumber = dr[Rowilpnumber].ToString();

		if (dr[Rowletterreference].ToString() != "")
			this.letterreference = dr[Rowletterreference].ToString();

		if (dr[Rowletternotes].ToString() != "")
			this.letternotes = dr[Rowletternotes].ToString();

		if (dr[Rowletteruser].ToString() != "")
			this.letteruser = dr[Rowletteruser].ToString();

		if (dr[Rowsenttoprimaryemail].ToString() != "")
			this.senttoprimaryemail = dr[Rowsenttoprimaryemail].ToString();

		if (dr[Rowsenttosecondaryemail].ToString() != "")
			this.senttosecondaryemail = dr[Rowsenttosecondaryemail].ToString();
          }
          success = true;
        }
      }
      return success;
    }

    private bool SetClassPropertiesListFromDataSet(DataSet resultDS)
    {
      bool success = false;
      if (resultDS.Tables[0] != null)
      {
        if (resultDS.Tables[0].Rows.Count > 0)
        {
          foreach (DataRow dr in resultDS.Tables[0].Rows)
          {
            DOTletters Tletters = new DOTletters();
			
					if (dr[Rowletterid].ToString() != "")
			Tletters.letterid = int.Parse(dr[Rowletterid].ToString());

		if (dr[Rowilpnumber].ToString() != "")
			Tletters.ilpnumber = dr[Rowilpnumber].ToString();

		if (dr[Rowletterreference].ToString() != "")
			Tletters.letterreference = dr[Rowletterreference].ToString();

		if (dr[Rowletternotes].ToString() != "")
			Tletters.letternotes = dr[Rowletternotes].ToString();

		if (dr[Rowletteruser].ToString() != "")
			Tletters.letteruser = dr[Rowletteruser].ToString();

		if (dr[Rowsenttoprimaryemail].ToString() != "")
			Tletters.senttoprimaryemail = dr[Rowsenttoprimaryemail].ToString();

		if (dr[Rowsenttosecondaryemail].ToString() != "")
			Tletters.senttosecondaryemail = dr[Rowsenttosecondaryemail].ToString();

            this.Tletterss.Add(Tletters);
          }
          success = true;
        }
      }
      return success;
    }
    #endregion

    #region Class Support Methods
    #endregion
  }
}
